using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Web.Services.Protocols;

// Project Server 2007 References
using Microsoft.Office.Project.Server.Library;

namespace Microsoft.EPM.Events
{
    class GeneralHelper
    {
        /// <summary>
        /// Wait in milliseconds
        /// </summary>
        /// <param name="waitTime">milleseconds</param>
        public static void Wait(long waitTime)
        {
            long tics = 0;
            Stopwatch timePerOp = new Stopwatch();
            timePerOp.Start();
            do
            {
                tics = timePerOp.ElapsedMilliseconds;
            } while (tics < waitTime);
            timePerOp.Stop();
        }

        /// <summary>
        /// Format time for log entry
        /// </summary>
        /// <returns>date string</returns>
        public static string FormatTimeLog(bool byTheHour)
        {
            DateTime dtNow = DateTime.Now;
            String Year = dtNow.Year.ToString();
            String Month = FormatTwoDigits(dtNow.Month);
            String Day = FormatTwoDigits(dtNow.Day);
            String Hour = FormatTwoDigits(dtNow.Hour);
            String Minutes = FormatTwoDigits(dtNow.Minute);
            String Seconds = FormatTwoDigits(dtNow.Second);

            if (byTheHour)
                return Year + Month + Day + "-" + Hour;
            else
                return Year + Month + Day + "-" + Hour + Minutes + Seconds;
        }

        /// <summary>
        /// Format two digit
        /// </summary>
        /// <param name="source">datetine integer</param>
        /// <returns>datetime string</returns>
        public static string FormatTwoDigits(int source)
        {
            if (source < 10)
                return String.Format("0{0}", source.ToString());
            else
                return source.ToString();
        }

        /// <summary>
        /// Return string with PSI exceptions
        /// </summary>
        /// <param name="ex">Soap exceptions</param>
        /// <returns>string with error messages</returns>
        public static string HandleSoapException(SoapException ex)
        {
            PSClientError error = new PSClientError(ex);
            PSErrorInfo[] errors = error.GetAllErrors();
            string errMess = "==============================\r\nError: \r\n";
            for (int i = 0; i < errors.Length; i++)
            {
                errMess += "\n" + ex.Message.ToString() + "\r\n";
                errMess += "".PadRight(30, '=') + "\r\nPSCLientError Output:\r\n \r\n";
                errMess += errors[i].ErrId.ToString() + "\n";

                for (int j = 0; j < errors[i].ErrorAttributes.Length; j++)
                {
                    errMess += "\r\n\t" + errors[i].ErrorAttributeNames()[j] + ": "
                       + errors[i].ErrorAttributes[j];
                }
                errMess += "\r\n".PadRight(30, '=');
            }

            return errMess;
        }
    }
}
